package service

import (
	"file_explorer/common/consts"
	"file_explorer/common/log"
	"file_explorer/common/util"
	"file_explorer/model"
)

const (
	ConfFilename = "conf.yml"
)

var (
	defaultConf = &model.GlobalConf{
		Favorites:         nil,
		FavoritesNotExist: consts.FavoritesNotExistNotify,
		ShowVirtualFile:   false,
		MetaFileNum:       10,
		LogRowNum:         200,
	}
)

func ReadGlobalConf() *model.GlobalConf {
	conf := &model.GlobalConf{}
	err := util.ReadYmlFile(util.PathJoin(util.GetWorkDir(), ConfFilename), conf)
	if err != nil {
		log.Error("read global conf failed, err=%v", err)
		return defaultConf
	}

	log.Debug("read global conf success, conf=%+v", conf)

	return conf
}

func WriteGlobalConf(conf *model.GlobalConf) error {
	err := util.WriteYmlFile(conf, util.PathJoin(util.GetWorkDir(), ConfFilename))
	if err != nil {
		log.Error("update global conf failed, conf=%+v, err=%v", conf, err)
		return err
	}

	log.Debug("update global config success, conf=%+v", conf)

	return nil
}
